package com.nk.system.servlets;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.nk.system.beans.UserProfileBean;
import com.nk.system.service.AuthenticationService;

/**
 * Servlet implementation class UpdateUserServlet
 */
public class UpdateUserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UpdateUserServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		initial(request, response);
		return;
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		initial(request, response);
		return;
	}
	
	private void initial(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{
		
		HttpSession userSess = request.getSession();
		int id = (int)userSess.getAttribute("userID");
		
		String user = request.getParameter("username");
		String pass1 = request.getParameter("password1");
		String pass2 = request.getParameter("password2");
		String fName = request.getParameter("firstName");
		String lName = request.getParameter("lastName");
		String sex = request.getParameter("sex");
		/*String day = request.getParameter("day");
		String month = request.getParameter("month");
		String year = request.getParameter("year");*/
		String mail = request.getParameter("mail");
		String school = request.getParameter("school");
		String level = request.getParameter("level");
		String grade = request.getParameter("grade");
		
		int level2 = Integer.parseInt(level);
		float grade2 = Float.parseFloat(grade);
		
		WebApplicationContext con = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getServletContext());
		AuthenticationService authenticationService = (AuthenticationService)con.getBean("authenticationService");
		authenticationService.setUpdateUserProfile(id, fName, lName, sex, school, level2, grade2);
		
		request.getRequestDispatcher("LoginServlet").forward(request, response);
	
	}

}
